#ifndef HTTP_RESPONSE_H
#define HTTP_RESPONSE_H

#include <muduo/net/TcpServer.h>
#include <map>
#include <string>
#include "HttpRequest.h"

namespace http
{
    class HttpResponse
    {
    public:
        enum HttpStatus
        {
            Unknown = 0,                    // 未知状态
            Ok = 200,               // 请求成功
            NoContent = 204,        // 成功，但无内容返回
            MovedPermanently = 301, // 永久重定向
            BadRequest = 400,       // 错误的请求
            Unauthorized = 401,     // 未授权
            Forbidden = 403,        // 禁止访问
            NotFound = 404,         // 资源未找到
            Conflict = 409,         // 请求冲突
            InternalServerError = 500, // 服务器内部错误
        };
        HttpResponse(bool close = true);
        void setVersion(const std::string &version) { _version = version; }
        void setStatusCode(HttpStatus status_code) { _status_code = status_code; }
        HttpStatus getStatusCode() const { return _status_code; }
        void setStatusMessage(const std::string &status_message) { _status_message = status_message; }
        void addHeader(const std::string &key, const std::string &value) { _headers[key] = value; }
        void setCloseConnection(bool close) { _close_connection = close; }
        bool isCloseConnection() const { return _close_connection; }
        void setContentType(const std::string &content_type) { addHeader("Content-Type", content_type); }
        void setContentLength(size_t length) { addHeader("Content-Length", std::to_string(length)); }
        void setBody(const std::string &body) { _content = body; }
        const std::string &getBody() const { return _content; }
        void setStatusLine(const std::string &version, HttpStatus status_code, const std::string &status_message);
        void appendToBuffer(muduo::net::Buffer *outputBuf) const;

    private:
        std::string _version;                        // 响应HTTP版本
        HttpStatus _status_code;                            // 响应状态码
        std::string _status_message;                 // 响应状态消息
        std::map<std::string, std::string> _headers; // 响应头
        std::string _content;                        // 响应体
        bool _close_connection;                      // 是否是短连接
    };
}

#endif // HTTP_RESPONSE_H